package com.gitee.webdav.configure;

import com.gitee.webdav.WebdavApplication;
import com.gitee.webdav.kernel.user.AuthenticationProvider;
import com.gitee.webdav.kernel.resource.ResourceRepository;
import com.gitee.webdav.kernel.WebdavServlet;
import com.gitee.webdav.kernel.serializer.XmlSerializer;
import com.gitee.webdav.kernel.resource.FileResourceRepository;
import com.gitee.webdav.kernel.serializer.JacksonXmlSerializer;
import com.gitee.webdav.kernel.user.NoneAuthenticationProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author hongda.li
 */
@Configuration
@EnableConfigurationProperties(WebdavProperties.class)
@ComponentScan(basePackageClasses = WebdavApplication.class)
public class WebdavConfiguration {

    @Bean
    public ServletRegistrationBean<WebdavServlet> servletRegistrationBean() {
        return new ServletRegistrationBean<>(new WebdavServlet(), "/*");
    }

    @Bean
    @ConditionalOnMissingBean
    public XmlSerializer xmlSerializer() {
        return new JacksonXmlSerializer();
    }

    @Bean
    @ConditionalOnMissingBean
    public ResourceRepository<?> resourceRepository() {
        return new FileResourceRepository();
    }

    @Bean
    @ConditionalOnMissingBean
    public AuthenticationProvider<?> authenticationProvider() {
        return new NoneAuthenticationProvider();
    }
}
